package com.appbee.gdata.picasa.client.activity;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import com.appbee.core.client.data.BufferedDataProvider;
import com.appbee.core.client.data.Fetch;
import com.appbee.core.client.data.IDataUpdater;
import com.appbee.core.client.data.IFetcher;
import com.appbee.core.client.data.ISortInfo;
import com.appbee.gdata.picasa.shared.PicasaRequestFactory;
import com.appbee.gdata.picasa.shared.SimplePhotoProxy;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.Range;
import com.google.web.bindery.requestfactory.shared.Receiver;

@Singleton
public class PicasaPhotoDataProvider extends
		AsyncDataProvider<SimplePhotoProxy> implements
		IFetcher<SimplePhotoProxy>, IDataUpdater<SimplePhotoProxy>
{

	private final PicasaRequestFactory serviceProvider;
	private String albumid;
	private final BufferedDataProvider<SimplePhotoProxy> buffer = new BufferedDataProvider<SimplePhotoProxy>();
	private int size;
	private boolean exact;

	@Inject
	public PicasaPhotoDataProvider(PicasaRequestFactory serviceProvider)
	{
		super();
		this.serviceProvider = serviceProvider;
		buffer.setFetcher(this);
		buffer.setUpdater(this);
	}

	public String getAlbumid()
	{
		return albumid;
	}

	public void setAlbumid(String albumid)
	{
		buffer.setFilter(albumid);
		this.albumid = albumid;
	}

	@Override
	public void onRangeChanged(HasData<SimplePhotoProxy> display)
	{
		buffer.onRangeChange(display);
	}

	public void findById(String id, Receiver<? super SimplePhotoProxy> receiver)
	{
		serviceProvider.getPicasaService().findPhotoById(id).fire(receiver);
	}

	public int findPhotoIndexById(String id)
	{
		List<Fetch<SimplePhotoProxy>> fetchList = buffer.getFetchList();
		for (Fetch<SimplePhotoProxy> fetch : fetchList)
		{
			List<SimplePhotoProxy> data = fetch.getData();
			for (int i = 0; i < data.size(); i++)
			{
				SimplePhotoProxy simplePhotoProxy = data.get(i);
				if (id.equals(simplePhotoProxy.getUrl()))
					return i + fetch.getRange().getStart();
			}
		}
		return -1;
	}

	public SimplePhotoProxy getPhotoByIndex(int index)
	{
		List<Fetch<SimplePhotoProxy>> fetchList = buffer.getFetchList();
		for (Fetch<SimplePhotoProxy> fetch : fetchList)
		{
			int start = fetch.getRange().getStart();
			int end = start + fetch.getRange().getLength();
			if (fetch.getFilterHashCode() == albumid.hashCode()
					&& index >= start && index <= end)
			{
				return fetch.getData().get(index - start);
			}
		}
		return null;
	}

	public int getTotalsize()
	{
		return size;
	}

	public boolean isExact()
	{
		return exact;
	}

	@Override
	public void fetch(Range range, ISortInfo sortInfo,
			Receiver<List<SimplePhotoProxy>> receiver)
	{
		serviceProvider.getPicasaService()
				.findPhotos(albumid, range.getStart(), range.getLength())
				.fire(receiver);
	}

	@Override
	public void updateRowCount(int size, boolean exact)
	{

		this.size = size;
		this.exact = exact;
		super.updateRowCount(size, exact);
	}

	@Override
	public void updateRowData(int start, List<SimplePhotoProxy> values)
	{
		super.updateRowData(start, values);
	}

	public BufferedDataProvider<SimplePhotoProxy> getBuffer()
	{
		return buffer;
	}

}
